package com.zx.cs.controller;

import java.util.List;
import java.util.Objects;

import com.zx.cs.domain.InventoryReplenish;
import com.zx.cs.service.IInventoryReplenishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.zx.common.annotation.Log;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.enums.BusinessType;
import com.zx.common.utils.poi.ExcelUtil;
import com.zx.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 库存补货记录Controller
 *
 * @author ruoyi
 * @date 2025-09-05
 */
@RestController
@RequestMapping("cs/wm/rep/replenish")
public class InventoryReplenishController extends BaseController {

    @Autowired
    private IInventoryReplenishService inventoryReplenishService;

    /**
     * 查询库存补货记录列表
     */
    @PreAuthorize("@ss.hasPermi('replenish:list')")
    @GetMapping("/list")
    public TableDataInfo list(InventoryReplenish inventoryReplenish) {
        startPage();
        List<InventoryReplenish> list = inventoryReplenishService.selectInventoryReplenishList(inventoryReplenish);
        return getDataTable(list);
    }

    /**
     * 根据id,获取库存补货记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('replenish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success().success(inventoryReplenishService.selectInventoryReplenishById(id));
    }

    /**
     * 新增库存补货记录
     */
    @PreAuthorize("@ss.hasPermi('replenish:add')")
    @Log(title = "库存补货记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Valid InventoryReplenish inventoryReplenish) {
        try {
            return success().success(inventoryReplenishService.insertInventoryReplenish(inventoryReplenish));
        } catch (RuntimeException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改库存补货记录
     */
    @PreAuthorize("@ss.hasPermi('replenish:edit')")
    @Log(title = "库存补货记录", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody @Valid InventoryReplenish inventoryReplenish) {
        try {
            return success().success(inventoryReplenishService.updateInventoryReplenish(inventoryReplenish));
        } catch (RuntimeException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除库存补货记录
     */
    @PreAuthorize("@ss.hasPermi('replenish:remove')")
    @Log(title = "库存补货记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(inventoryReplenishService.deleteInventoryReplenishByIds(ids));
    }

    /**
     * 导出库存补货记录列表
     */
    @PreAuthorize("@ss.hasPermi('replenish:export')")
    @Log(title = "库存补货记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InventoryReplenish inventoryReplenish) {
        List<InventoryReplenish> list = inventoryReplenishService.selectInventoryReplenishList(inventoryReplenish);
        ExcelUtil<InventoryReplenish> util = new ExcelUtil<InventoryReplenish>(InventoryReplenish.class);
        util.exportExcel(response, list, "库存补货记录数据");
    }

}
